# encoding=utf-8

import time, json, re
import config, instruction
from bot2 import Bot

def prepare(bot):
    pass

def exercise(bot):
    bot.send('{"id":"100051","cmd":"exercise 1000"}')
    rr = bot.recv_data()
    resp = json.loads(rr)
    ## {"info":[{"text":"你运转内息遍布全身时却没有感觉到内力提升的迹象，看来你基本内功等级不够。\n"}],"id":"100052","title":"修炼内力"}
    try:
        if resp['id'] == '100052':
            info = resp['info'][0]['text'].encode('utf-8')
            if re.search('你太累了，根本没法修炼内力', info):
                print '生命不足，前往睡觉'
                goto_sleep(bot)
            elif re.search('你专心修炼内力，似乎有一些进展', info) or re.search('你的内力提高了', info):
                print '当前内力值：[%s]' % re.split('\n', info)[0]
            else:
                pass
    except Exception as ex:
        print '解析消息失败: %s' % ex


def goto_sleep(bot):
    bot.send_ins(instruction.to_kezhan())
    bot.send('{"id":"100051","cmd":"sleep"}')
    bot.recv_data()
    time.sleep(2)


def main():
    user = config.users[0]
    bot = Bot('heeejianbo', 'Wskhebo1994')
    #bot = Bot('pytest002', '123456')
    bot.set_login_addr(config.login_server)
    bot.set_register_addr(config.regsi_server)

    bot.set_prepare(prepare)
    bot.set_task(exercise, 0.2)

    bot.start()

main()
